package han.chensing.latos.maker.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.utils.ScreenUtils;

import han.chensing.latos.maker.GameMain;
import han.chensing.latos.maker.res.LoadEntry;
import han.chensing.latos.maker.res.Res;
import han.chensing.latos.maker.V;

public class WelcomeScreen implements Screen {

    ShapeRenderer shapeRenderer;
    SpriteBatch spriteBatch;
    Color color1 = Color.valueOf("#52B4FFFF");
    Color color2=Color.valueOf("#32CD32FF");
    Texture loadingImage,loadingText;
    BitmapFont loadBitmapFont;

    public WelcomeScreen() {
        shapeRenderer=new ShapeRenderer();
        spriteBatch=new SpriteBatch();
        Res.get().fakeLoadAll();
        loadingImage=new Texture(Gdx.files.internal("loading/daiyousei.png"),true);
        loadingImage.setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear);
        loadingText=new Texture(Gdx.files.internal("loading/loadingText.png"),true);
        loadingText.setFilter(Texture.TextureFilter.MipMapLinearLinear, Texture.TextureFilter.Linear);
        loadBitmapFont=new BitmapFont(Gdx.files.internal("loading/load.fnt"));
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        ScreenUtils.clear(0.2f, 0.2f, 0.3f, 1);
        LoadEntry loadEntry = Res.get().readLoadSingle();
        if (loadEntry==null) {
            GameMain.getInstance().loadingComplete();
        }
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        Matrix4 projectionMatrix = V.viewport.getCamera().combined;
        shapeRenderer.setProjectionMatrix(projectionMatrix);
        float loadPercent = (float)Res.get().getLoadedTotal() / (float) Res.get().getLoadTotal();
        shapeRenderer.rect(0,0,loadPercent* V.WIDTH,7, color2, color1, color1, color2);
        shapeRenderer.end();
        spriteBatch.setProjectionMatrix(projectionMatrix);
        spriteBatch.begin();
        spriteBatch.draw(loadingImage,75,140);
        spriteBatch.draw(loadingText,20,30);
        loadBitmapFont.setColor(mixColor(loadPercent));
        if (loadEntry!=null)
            loadBitmapFont.draw(spriteBatch,"Loading "+loadEntry.type.name()+":"+loadEntry.name,15,35);
        spriteBatch.end();
    }

    private Color mixColor(float percent){
        Color color=new Color(Color.BLACK);
        color.r=color2.r*percent+color1.r*(1f-percent);
        color.g=color2.g*percent+color1.g*(1f-percent);
        color.b=color2.b*percent+color1.b*(1f-percent);
        return color;
    }

    @Override
    public void resize(int width, int height){
		V.container.onResize(width,height);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        shapeRenderer.dispose();
        loadingImage.dispose();
        loadingText.dispose();
        spriteBatch.dispose();
        loadBitmapFont.dispose();
    }
}
